﻿using log4net;
using MyWebApp.Areas.Sys.Models;
using MyWebApp.Common;
using MyWebApp.Common.Entity;
using MyWebApp.Common.UIAttribute;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;

namespace MyWebApp.Areas.Sys.Controllers
{
    public class UserController : BaseMvcController
    {
        // GET: Sys/User
        private static ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        private static List<UserModel> udata = new List<UserModel>();

        public UserController()
        {
            if (udata.Count == 0)
            {
                for (int i = 0; i < 102; i++)
                {
                    udata.Add(new UserModel() { Id = i, UserId = "U_" + i.ToString(), FullName = "用户_" + i.ToString(), Sex = "男", Phone = (13501010001 + i).ToString(), Mail = "1@163.com", CreateUser = "admin", CreateTime = DateTime.Now, State = 1 });
                }
            }
        }

        // GET: User
        [Layout]
        public ActionResult Index()
        {
            return View();
        }

        public JsonResult GetPageData(string UserName, DtSearchParam param)
        {
            var data = udata;
            if (!string.IsNullOrEmpty(UserName))
            {
                data = data.Where(p => !string.IsNullOrEmpty(p.FullName) && p.FullName.Contains(UserName)).ToList();
            }
            DtData<UserModel> pageData = new DtData<UserModel>();
            pageData.data = data.Skip(param.start).Take(param.length);
            pageData.recordsFiltered = data.Count;
            //pageData.iTotalRecords = data.Count; //(int)Math.Ceiling((double)data.Count / length);

            return this.JsonNet(pageData, JsonRequestBehavior.AllowGet);
        }

        public JsonResult Add([FromBody]UserModel model)
        {
            ApiResult result = new ApiResult() { Code = 0, Method = "Add" };
            try
            {
                if (udata.Count(p => p.UserId == model.UserId) > 0)
                {
                    result.Msg = string.Format("User:{0}已经存在", model.UserId);
                }
                else
                {
                    udata.Add(model);
                    result.Code = 1;
                }
            }
            catch (Exception ex)
            {
                log.Warn("执行失败", ex);
                result.Msg = "执行失败:" + ex.Message;
            }
            return Json(result);
        }

        public ContentResult CheckUseridExists(string Userid)
        {
            if (udata.Count(p => p.UserId == Userid) > 0)
            {
                return Content("false");
            }
            else
            {
                return Content("true");
            }
        }

        public JsonResult Delete(string uid)
        {
            ApiResult result = new ApiResult() { Code = 0, Method = "Add" };
            try
            {
                var idx = udata.FindIndex(p => p.UserId == uid);
                if (idx >= 0)
                {
                    udata.RemoveAt(idx);
                }
                result.Code = 1;
            }
            catch (Exception ex)
            {
                log.Warn("执行失败", ex);
                result.Msg = "执行失败:" + ex.Message;
            }
            return Json(result);
        }
    }
}